import axios from 'axios';
import { Base64 } from 'js-base64';

export async function expandRowsH(product_code,product_name,startDate,endDate) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "SELECT t.product_code,t.product_name,t.degree_of_staining_code, \n" +
            "                        t.degree_of_staining_name,count(1) as detail_count, \n" +
            "                        case when count(1) = 0 then 0 else round(sum(t.loss_rate)/count(1),2) end AS avg_loss_rate\n" +
            "                         FROM (SELECT t_dyelot_code,\n" +
            "                          t_product_code as product_code,\n" +
            "                          t_product_name as product_name,\n" +
            "                          t_label_color_name as label_color_name,\n" +
            "                          t_fabric_sum as fabric_sum,\n" +
            "                          t_fabric_label_weight_sum as fabric_label_weight_sum,\n" +
            "                          t_pi_sum as pi_sum,\n" +
            "                          t_xima_sum as xima_sum,\n" +
            "                          t_kongcha as kongcha,\n" +
            "                          t_tube_weight as tube_weight,\n" +
            "                          t_loss_rate as loss_rate,\n" +
            "                          t_remarks as remarks,\n" +
            "                          t_degree_of_staining_code as degree_of_staining_code,\n" +
            "                          t_degree_of_staining_name as degree_of_staining_name,\n" +
            "                          FORMAT(t_in_date, 'yyyy-MM-dd') as in_date\n" +
            "                         from t_io_doc_loss \n" +
            "                        where t_in_date BETWEEN CONVERT(datetime, '"+startDate+" 00:00:00') and CONVERT(datetime, '"+endDate+" 23:59:59')  \n" +
            "                           and t_product_name like '%"+product_name+"%' and t_product_code like '%"+product_code+"%' ) t  \n" +
            "                          GROUP BY t.product_code,t.product_name,t.degree_of_staining_code,t.degree_of_staining_name";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responsedatas = response.data.data;
        return responsedatas;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}
export async function expandRowsExportD(product_code,product_name,startDate,endDate) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "SELECT t_dyelot_code as dyelot_code,\n" +
            "                          t_product_code as product_code,\n" +
            "                          t_product_name as product_name,\n" +
            "                          t_degree_of_staining_code as degree_of_staining_code,\n" +
            "                          t_degree_of_staining_name as degree_of_staining_name,\n" +
            "                          t_label_color_name as label_color_name,\n" +
            "                          t_fabric_sum as fabric_sum,\n" +
            "                          t_fabric_label_weight_sum as fabric_label_weight_sum,\n" +
            "                          t_pi_sum as pi_sum,\n" +
            "                          t_xima_sum as xima_sum,\n" +
            "                          t_kongcha as kongcha,\n" +
            "                          t_tube_weight as tube_weight,\n" +
            "                          t_loss_rate as loss_rate,\n" +
            "                          t_remarks as remarks,\n" +
            "                          t_in_date as in_date\n" +
            "                       from t_io_doc_loss \n" +
            "                       where t_in_date BETWEEN CONVERT(datetime, '"+startDate+" 00:00:00') and CONVERT(datetime, '"+endDate+" 23:59:59')\n" +
            "                       and t_product_name like '%"+product_name+"%'  and t_product_code like '%"+product_code+"%' ";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );

        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}

export async function expandRowsD(product_code,degree_of_staining_code,startDate,endDate) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "SELECT t_dyelot_code as dyelot_code,\n" +
            "              t_product_code as product_code,\n" +
            "              t_product_name as product_name,\n" +
            "              t_degree_of_staining_code as degree_of_staining_code,\n" +
            "              t_degree_of_staining_name as degree_of_staining_name,\n" +
            "              t_label_color_name as label_color_name,\n" +
            "              t_fabric_sum as fabric_sum,\n" +
            "              t_fabric_label_weight_sum as fabric_label_weight_sum,\n" +
            "              t_pi_sum as pi_sum,\n" +
            "              t_xima_sum as xima_sum,\n" +
            "              t_kongcha as kongcha,\n" +
            "              t_tube_weight as tube_weight,\n" +
            "              t_loss_rate as loss_rate,\n" +
            "              t_remarks as remarks,\n" +
            "              FORMAT(t_in_date, 'yyyy-MM-dd') as in_date\n" +
            "          from t_io_doc_loss \n" +
            "           where t_product_code = '"+product_code+"' and t_degree_of_staining_code = '"+degree_of_staining_code+"' \n" +
            "           and t_in_date BETWEEN CONVERT(datetime, '"+startDate+" 00:00:00') and CONVERT(datetime, '"+endDate+" 23:59:59')  \n"
            ;
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );

        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}


export async function getMachineDetailss(machinename,batchname) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "select DISTINCT m.t_machinename as t_machinename,\n" +
            "                m.t_batchname as t_batchname,\n" +
            "                FORMAT(m.t_supcon_time,'HH:mm:ss') as t_supcon_time,\n" +
            "                m.t_machinestate as t_machinestate,\n" +
            "                m.t_machinemode as t_machinemode,\n" +
            "                isnull(mr.t_water_vol_tota_numberl,m.t_water_vol_tota_numberl) as t_water_vol_total_number,\n" +
            "                isnull(mr.t_loading,m.t_loading) as t_loading,\n" +
            "                ROUND(isnull(mr.t_water_vol_tota_numberl,m.t_water_vol_tota_numberl)/isnull(mr.t_loading,m.t_loading), 6)*1000 as t_loading_rate\n" +
            "             from t_supcon_machine_Info m\n" +
            "             left join t_dyelot_info df on m.t_batchname = df.t_dyelot_code\n" +
            "             left join t_supcon_schedule_report mr \n" +
            "              on m.t_machinename = mr.t_machinename and m.t_batchname = mr.t_userbatchno  \n" +
            "              and m.t_supcon_time BETWEEN  mr.t_startdatetime and mr.t_enddatetime  \n" +
            "             where m.t_machinename = '"+machinename+"' and m.t_batchname = '"+batchname+"' \n" +
            "              and FORMAT(m.t_create , 'yyyy-MM-dd') = FORMAT(DATEADD(DAY, -1, GETDATE()), 'yyyy-MM-dd')  \n" +
            "              and FORMAT(m.t_supcon_time , 'yyyy-MM-dd') = FORMAT(DATEADD(DAY, -1, GETDATE()), 'yyyy-MM-dd')  \n" +
            "             order by m.t_machinename,FORMAT(m.t_supcon_time,'HH:mm:ss'),m.t_batchname";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );

        const responseData = response.data;
        return responseData.data;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}

export async function getSupconDatas() {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "select DISTINCT m.t_machinename as t_machinename,\n" +
            "    CONVERT(varchar(3),m.t_tubes)+'管' as t_tubes,\n" +
            "    mr.t_dey_number as t_dey_number,\n" +
            "    m.t_supcon_time as t_supcon_time,\n" +
            "    m.t_batchname as t_batchname,\n" +
            "    m.t_machinestate as t_machinestate,\n" +
            "    m.t_machinemode as t_machinemode,\n" +
            "    df.t_dyeing_color_name as t_dyeing_color_name,\n" +
            "    df.t_product_code as t_product_code,\n" +
            "    m.t_sub_dyeprogname_01 as t_sub_dyeprogname_01,\n" +
            "    m.t_sub_dyeprogname_02 as t_sub_dyeprogname_02,\n" +
            "    m.t_sub_dyeprogname_03 as t_sub_dyeprogname_03,\n" +
            "    m.t_water_vol1_total_number as t_water_vol1_total_number,\n" +
            "    m.t_water_vol2_total_number as t_water_vol2_total_number,\n" +
            "    m.t_water_vol3_tota_numberl as t_water_vol3_total_number,\n" +
            "    m.t_water_vol4_tota_numberl as t_water_vol4_total_number,\n" +
            "    m.t_water_vol_tota_numberl as t_water_vol_total_number,\n" +
            "    m.t_loading as t_loading,\n" +
            "    ROUND((m.t_water_vol_tota_numberl)/m.t_loading, 6)*1000 as t_loading_rate,\n" +
            "    CONVERT(varchar, mr.t_startdatetime, 120) as t_startdatetime,\n" +
            "    CONVERT(varchar, mr.t_enddatetime, 120) as t_enddatetime,\n" +
            "    CONVERT(VARCHAR(10),CAST(ROUND(DATEDIFF(SECOND,mr.t_startdatetime, mr.t_enddatetime) / (24.0 * 60.0 * 60.0), 0) AS INT)) + '天' + \n" +
            "    CONVERT(VARCHAR(10),CAST((ROUND(DATEDIFF(SECOND,mr.t_startdatetime, mr.t_enddatetime) / (60.0 * 60.0), 0) % 24) AS INT)) + '小时' + \n" +
            "    CONVERT(VARCHAR(10),CAST((ROUND(DATEDIFF(SECOND,mr.t_startdatetime, mr.t_enddatetime) / 60.0, 0) % 60) AS INT)) + '分钟' + \n" +
            "    CONVERT(VARCHAR(10),CAST((ROUND(DATEDIFF(SECOND,mr.t_startdatetime, mr.t_enddatetime), 0) % 60) AS INT)) + '秒' AS t_runtime \n" +
            " from t_supcon_machine_Info m left join t_dyelot_info df on m.t_batchname = df.t_dyelot_code \n" +
            " left join t_supcon_schedule_report mr \n" +
            "  on m.t_machinename = mr.t_machinename and m.t_batchname = mr.t_userbatchno  \n" +
            "  and m.t_supcon_time BETWEEN  mr.t_startdatetime and mr.t_enddatetime  \n" +
            "where FORMAT(m.t_create , 'yyyy-MM-dd') = FORMAT(DATEADD(DAY, -1, GETDATE()), 'yyyy-MM-dd')  \n" +
            "  and FORMAT(m.t_supcon_time , 'yyyy-MM-dd') = FORMAT(DATEADD(DAY, -1, GETDATE()), 'yyyy-MM-dd')  \n" +
            " order by m.t_machinename,m.t_supcon_time,m.t_batchname";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}

export async function getSupconDatasD(machinename,batchname) {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "select DISTINCT m.t_machinename as t_machinename,\n" +
            "    CONVERT(varchar(3),m.t_tubes)+'管' as t_tubes,\n" +
            "    mr.t_dey_number as t_dey_number,\n" +
            "    CONVERT(varchar, m.t_supcon_time, 120) as t_supcon_time,\n" +
            "    m.t_batchname as t_batchname,\n" +
            "    m.t_machinestate as t_machinestate,\n" +
            "    m.t_machinemode as t_machinemode,\n" +
            "    df.t_dyeing_color_name as t_dyeing_color_name,\n" +
            "    df.t_product_code as t_product_code,\n" +
            "    m.t_sub_dyeprogname_01 as t_sub_dyeprogname_01,\n" +
            "    m.t_sub_dyeprogname_02 as t_sub_dyeprogname_02,\n" +
            "    m.t_sub_dyeprogname_03 as t_sub_dyeprogname_03,\n" +
            "    m.t_water_vol1_total_number as t_water_vol1_total_number,\n" +
            "    m.t_water_vol2_total_number as t_water_vol2_total_number,\n" +
            "    m.t_water_vol3_tota_numberl as t_water_vol3_total_number,\n" +
            "    m.t_water_vol4_tota_numberl as t_water_vol4_total_number,\n" +
            "    m.t_water_vol_tota_numberl as t_water_vol_total_number,\n" +
            "    m.t_loading as t_loading,\n" +
            "    ROUND((m.t_water_vol_tota_numberl)/m.t_loading, 6)*1000 as t_loading_rate,\n" +
            "    CONVERT(varchar, mr.t_startdatetime, 120) as t_startdatetime,\n" +
            "    CONVERT(varchar, mr.t_enddatetime, 120) as t_enddatetime,\n" +
            "    CONVERT(VARCHAR(10),CAST(ROUND(DATEDIFF(SECOND,mr.t_startdatetime, mr.t_enddatetime) / (24.0 * 60.0 * 60.0), 0) AS INT)) + '天' + \n" +
            "    CONVERT(VARCHAR(10),CAST((ROUND(DATEDIFF(SECOND,mr.t_startdatetime, mr.t_enddatetime) / (60.0 * 60.0), 0) % 24) AS INT)) + '小时' + \n" +
            "    CONVERT(VARCHAR(10),CAST((ROUND(DATEDIFF(SECOND,mr.t_startdatetime, mr.t_enddatetime) / 60.0, 0) % 60) AS INT)) + '分钟' + \n" +
            "    CONVERT(VARCHAR(10),CAST((ROUND(DATEDIFF(SECOND,mr.t_startdatetime, mr.t_enddatetime), 0) % 60) AS INT)) + '秒' AS t_runtime \n" +
            " from t_supcon_machine_Info m left join t_dyelot_info df on m.t_batchname = df.t_dyelot_code \n" +
            " left join t_supcon_schedule_report mr \n" +
            "  on m.t_machinename = mr.t_machinename and m.t_batchname = mr.t_userbatchno  \n" +
            "  and m.t_supcon_time BETWEEN  mr.t_startdatetime and mr.t_enddatetime  \n" +
            "where FORMAT(m.t_create , 'yyyy-MM-dd') = FORMAT(DATEADD(DAY, -1, GETDATE()), 'yyyy-MM-dd')  \n" +
            "  and FORMAT(m.t_supcon_time , 'yyyy-MM-dd') = FORMAT(DATEADD(DAY, -1, GETDATE()), 'yyyy-MM-dd')  \n" +
            "  and m.t_machinename = '"+machinename+"' and m.t_batchname = '"+batchname+"' \n" +
            " order by m.t_machinename,CONVERT(varchar, m.t_supcon_time, 120),m.t_batchname";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error; // 抛出错误以便在组件中处理
    }
}

export async function getSupconDatasH() {
    try {
        // 使用模板字符串或参数绑定来构造SQL语句（取决于你的后端API如何处理参数）
        const sqlStatement = "select m.t_machinename as t_machinename,\n" +
            "                    CONVERT(varchar(3),m.t_tubes)+'管' as t_tubes,\n" +
            "                    m.t_batchname as t_batchname,\n" +
            "                    ROUND(max(m.t_water_vol_tota_numberl)/max(m.t_loading)*1000,2) as t_load_rate\n" +
            "                 from t_supcon_machine_Info m \n" +
            "                where FORMAT(m.t_create , 'yyyy-MM-dd') = FORMAT(DATEADD(DAY, -1, GETDATE()), 'yyyy-MM-dd') \n" +
            "                  and FORMAT(m.t_supcon_time , 'yyyy-MM-dd') = FORMAT(DATEADD(DAY, -1, GETDATE()), 'yyyy-MM-dd')  \n" +
            "                  group by m.t_machinename,m.t_tubes,m.t_batchname\n" +
            "                 order by m.t_machinename,m.t_batchname";
        const sql = Base64.encode(sqlStatement);
        // 发送POST请求到后端API，并传递参数
        const response = await axios.post(
            "http://8.134.79.23:34343/sql_actuator/execute",
            {
                "env": 'internet-ms-pe',
                "sql": sql,
            }
        );
        const responseData = response.data.data;
        return responseData;
    } catch (error) {
        console.error('Error fetching data:', error);
        throw error;
    }
}